export default{
    getVersionProperty : function(appVersion,appPath){
        var self = this;
        var ua = navigator.userAgent.toLowerCase();
        if(ua.match(/Android/i) != "android"){//非android系统不需要更新

            return ;
        }



        // 检查版本
        if(!window.plus)return ;
        // 检查当前版本，与从后台获取的版本作比较，以此判断是否更新     
        plus.runtime.getProperty(plus.runtime.appid,function(inf){
            // 当前版本
            var infVersion = inf.version;
            console.log("当前版本号："+infVersion+"--"+appVersion);
            if(appVersion == infVersion) return ;

            var gymd = this.getYYYYMMdd();
        
            var sgmyd = window.localStorage.getItem("upgradeYMD");
            console.log("当前日期："+gymd+"==="+sgmyd);
            if(gymd == sgmyd)return ;//当天提示过更新，则不再提示
    
            window.localStorage.setItem("upgradeYMD",gymd);

            // plus.nativeUI.showWaiting("检测更新版本...");
            console.log("更新路径："+appPath);
            plus.nativeUI.confirm("检测到有新版本",function(e){ 
                var i = e.index;
                console.log("点击了:"+i);
                if(i == 0 ){
                    self.downloadApp(appPath);
                }
             } , 
            "更新提示" , 
            ["立即更新","跳过此版本","取消",""]);
        });
            
    
    },
    downloadApp : function (wgtUrl){
    var self = this;
	// 更新文件 wgt 文件地址
	// var wgtUrl = "http://192.168.0.156/H503A1250.wgt";
    plus.nativeUI.showWaiting("正在更新...");
    var dtask = plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
     if ( status == 200 ) {
          console.log("下载apk成功："+d.filename);
          self.installApk(d.filename); // 安装wgt方法
      } else {
          console.log("下载apk失败！");
          plus.nativeUI.alert("下载apk失败！");
      }
      plus.nativeUI.closeWaiting();
  });
  
  
  //下载事件监控
    dtask.addEventListener("statechanged", function(task, status) {
        if(!dtask) {
            return;
        }
        switch(task.state) {
            case 0:
                console.log("未初始化!");
            break;
            case 1:
                console.log("开始下载!");
            break;
            case 2:
                console.log("连接到服务器!");
            break;
            case 3:
                console.log("接收数据!");
            break;
            case 4:
                console.log("下载完成!");
            break;
        }
    });
    dtask.start();
},


// 安装wgt方法
installApk:function(path) {
    plus.nativeUI.showWaiting("安装更新文件...");
    plus.runtime.install(path,{},function(){
        plus.nativeUI.closeWaiting();
        console.log("安装apk文件成功！");
        plus.nativeUI.alert("应用资源更新完成！",function(){
            plus.runtime.restart();
        });
    },function(e){ 
        plus.nativeUI.closeWaiting();
        console.log("安装apk文件失败["+e.code+"]："+e.message);
        plus.nativeUI.alert("安装apk文件失败["+e.code+"]："+e.message);
    });
},
getYYYYMMdd:function (){
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; 
    var curr_year = d.getFullYear();
    String(curr_month).length < 2 ? (curr_month = "0" + curr_month): curr_month;
    String(curr_date).length < 2 ? (curr_date = "0" + curr_date): curr_date;
    var yyyyMMdd = curr_year + "" + curr_month +""+ curr_date;
    return yyyyMMdd;
}

}


